/*-*
 * FILENAME  :
 *    $HeadURL$
 *
 * STATUS  :
 *    $Revision$
 *
 *    $Author$
 *    $Date$
 *  
 *    
 * Copyright (c) 2011 Gonalge Ltda. All rights reserved.
 *
 ****************************************************************/

package org.gonalge.arbolrojonegro.util;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTextField;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author Geremias Gonzalez
 */
public class FormUtils {

    private static final Log LOG = LogFactory.getLog(FormUtils.class);

    public static Rectangle crearRectangle(JFrame frame) {
        Rectangle rectangle = createRectangle(frame.getToolkit().getScreenSize());
        frame.setResizable(false);
        return rectangle;
    }

    private static Rectangle createRectangle(Dimension dim) {
        int widthForm = 1000;
        int heightForm = 760;
        int x = (dim.width - widthForm) / 2;
        int y = (dim.height - heightForm) / 2;
        return new Rectangle(x, y, widthForm, heightForm);
    }

    public static BufferedImage getImagen(Class<?> contenedor, String name) {
        try {
            return ImageIO.read(contenedor.getClassLoader().getResource("img/" + name + ".png"));
        } catch (Exception e) {
            LOG.error("Error de imagen: " + name, e);
            return null;
        }
    }

    public static ImageIcon getImageIcon(Class<?> contenedor, String name) {
        return new ImageIcon(getImagen(contenedor, name));
    }

    public static Color getColorFondo() {
        return new Color(179, 207, 252);
    }

    public static Color getColorTitulo() {
        return new Color(185, 0, 0);
    }

    public static void updateButton(Class<?> contenedor, JButton button, String imagenDef, String imagenSel, int x, int y) {
        button.setBorder(BorderFactory.createLineBorder(getColorFondo()));
        button.setBackground(getColorFondo());
        button.setIcon(FormUtils.getImageIcon(contenedor, imagenDef));
        button.setRolloverIcon(FormUtils.getImageIcon(contenedor, imagenSel));
        button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        button.setBounds(x, y, button.getIcon().getIconWidth() + 2, button.getIcon().getIconHeight() + 2);

    }

    public static void updateLabelPregunta(JLabel label, int x, int y) {
        label.setBounds(x, y, 435, 45);
        label.setFont(new Font("Tahoma", 1, 12));
    }

    public static void updateJSpinner(JSpinner spinner, int x, int y) {
        spinner.setBounds(x, y, 60, 20);
        spinner.setFont(new Font("Tahoma", 1, 12));
    }

    public static void updateJCheckBox(JCheckBox checkBox, int x, int y, boolean estado) {
        checkBox.setText(Boolean.toString(estado));
        checkBox.setBounds(x, estado ? y : y + 15, 60, 15);
        checkBox.setBackground(FormUtils.getColorFondo());
        checkBox.setFont(new Font("Tahoma", 1, 12));
        checkBox.setSelected(!estado);
    }

    public static void updateTextFieldNumero(JTextField textField, int x, int y) {
        textField.setBounds(x, y, 30, 20);
        textField.setFont(new Font("Tahoma", 1, 12));
        textField.setHorizontalAlignment(JTextField.RIGHT);

        textField.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                char caracter = e.getKeyChar();
                if (!Character.isDigit(caracter)) {
                    e.consume();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }
        });
    }

    public static void updateFontLavel(JComponent component) {
        component.setForeground(Color.BLACK);
        component.setFont(new Font("Tahoma", 1, 13));
    }
}
